//
//  Gunner.swift
//  ShapeSurvival
//
//  Created by Jerry H on 1/15/15.
//  Copyright (c) 2015 HeylmunProgramming. All rights reserved.
//

import Foundation; import SpriteKit

class Gunner: Enemy {
    var base: Enemy = Enemy(imageNamed: "GunnerBase")
    var turret: Enemy = Enemy(imageNamed: "GunnerTurret")
    //var base: SKSpriteNode = SKSpriteNode(imageNamed: "GunnerBase")
    //var turret: SKSpriteNode = SKSpriteNode(imageNamed: "GunnerTurret")
    var side = 0; // 1 top, 2 left, 3 bot, 4 right
    
    override func setEnemy(Xpos: CGFloat, Ypos: CGFloat, startingAngle: CGFloat){
        var newX = Xpos
        var newY = Ypos
        
        if(Ypos == highY || Ypos == lowY){
            //from top or bot
            if(Xpos > CGRectGetMaxX(playScene.frame) || Xpos < 0){
                newX = CGFloat(arc4random_uniform(UInt32(CGRectGetMaxX(playScene.frame)) - 100) + 50)
            }
        }
        else if(Ypos > CGRectGetMaxY(playScene.frame) || Ypos < 0){
            newY = CGFloat(arc4random_uniform(UInt32(CGRectGetMaxY(playScene.frame)) - 100 ) + 50)
        }
        label1.text = "oldX \(Xpos), oldY \(Ypos), newX \(newX), newY\(newY)"
        self.position.x = newX;
        self.position.y = newY;
        self.movespeed = 100
        turret.movespeed = 100
        base.movespeed = 100
        self.zPosition = 0.5
        self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        self.setRotation(startingAngle)
        base.setRotation(startingAngle)
        turret.setRotation(startingAngle)
        self.rotatesPerSecond = 1.5
        turret.rotatesPerSecond = 1.5
        base.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        turret.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        base.position = self.position
        turret.position = self.position
        turret.zRotation = self.zRotation
        base.zRotation = self.zRotation
        base.zPosition = 0.51
        turret.zPosition = 0.53
        eManager.currentEnemies.append(self)
        playScene.addChild(base)
        playScene.addChild(turret)
        
    }
    
    override func moveSelf(PlayerX: CGFloat, PlayerY: CGFloat) {
        if(self.position.x > CGRectGetMaxX(playScene.frame) || self.position.x < 0 || self.position.y > CGRectGetMaxY(playScene.frame) || self.position.y < 0 ){
            //label1.text = "here1 \(Int(self.position.x)), \(Int(self.position.y)), \(Int(player.position.x)), \(Int(player.position.y)),\(CGRectGetMaxX(playScene.frame)), \(CGRectGetMaxY(playScene.frame)) "
            //let move = moveByFacing(2.0)
            
            //self.runAction(self.moveWithoutFacing())
            //base.runAction(self.moveWithoutFacing())
            //turret.runAction(self.moveWithoutFacing())
            
            base.runAction(base.moveByFacing(0.0))
            turret.runAction(turret.moveByFacing(0.0))
            self.position = base.position
            
        }
        else{
            turret.runAction(turret.facePlayer())
            //label1.text = "here2\(Int(self.position.x)), \(Int(self.position.y)), \(Int(player.position.x)), \(Int(player.position.y)),\(CGRectGetMaxX(playScene.frame)), \(CGRectGetMaxY(playScene.frame)) "
  
        }
        //label1.text = "\(self.position) \n \(player.position)"
        //label1.text = "\(CGRectGetMaxX(playScene.frame)), \(CGRectGetMaxY(playScene.frame))"
    }
    
}
